package com.yuezhu.constomview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuezhu.Model.DictionaryModel;
import com.yuezhu.Model.PlanModel;
import com.yuezhu.R;
import com.yuezhu.global.ApplicationContext;

import java.util.ArrayList;

/**
 * Created by zhao on 2016/5/19.
 */
public class DragViewGroups extends LinearLayout {
    LinearLayout view;
    int screen_height = ApplicationContext.getInstance().getScreen_height();

    public DragViewGroups(Context context) {
        super(context);
        initview(context);
    }

    public DragViewGroups(Context context, AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    public DragViewGroups(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context);
    }

    private void initview(Context context) {
//        BusProvider.getInstance().register(this);
//        EventBus.getDefault().register(this);
        view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.dragview_group, this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        BusProvider.getInstance().unregister(this);
//        EventBus.getDefault().unregister(this);
    }

    private ArrayList<DragView> mDragViewList = new ArrayList<>();

    public void setViewCount(int n, boolean isPartInfo) {
        for (int i = 0; i < n; i++) {
            DragView dragView = new DragView(getContext(), isPartInfo);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (isPartInfo) {
                params.height = (int) (screen_height / 2.5);
            } else {
                params.height = (int) (screen_height / 2);
            }
            params.weight = 1;
            if (i > 0) {
                view.addView(new View(getContext()), new ViewGroup.LayoutParams(getResources().getDimensionPixelSize(R.dimen.drag_view_2dp), 100));
            }
            view.addView(dragView, params);
//            dragView.setLocationMinAndMax(52 + i * 4, 25 + i);
            dragView.setPosition(i);
            mDragViewList.add(dragView);
        }
    }

    public void setSkyLobby(int index, boolean is) {
        mDragViewList.get(index).setSkyLobby(is);
    }

    public void setView(ArrayList<PlanModel.Area> areaArrayList, int max) {
        for (View v : mDragViewList) {
            v.setVisibility(INVISIBLE);
        }
        for (int i = 0; i < areaArrayList.size(); i++) {
            mDragViewList.get(i).setMax(max);
            mDragViewList.get(i).setTag(areaArrayList.get(i));
            mDragViewList.get(i).setFenquText(areaArrayList.get(i).areaName);
            mDragViewList.get(i).setVisibility(VISIBLE);
            mDragViewList.get(i).setLocationMinAndMax(areaArrayList.get(i).floorTop, areaArrayList.get(i).floorBottom);
        }
    }

    public void setFenquText(String s, int index) {
        mDragViewList.get(index).setFenquText(s);
    }

    public void setViewSelected(int i) {
        for (DragView v : mDragViewList) {
            v.setSelected(false);
            PlanModel.Area area = (PlanModel.Area) v.getTag();
            if (area == null) {
                v.setColorView(Color.parseColor("#33cc99"));
            } else {
                if ("办公".equalsIgnoreCase(DictionaryModel.getInstance().getTypeByTypeId(area.type).typeName)) {
                    v.setColorView(Color.parseColor("#33cc99"));
                } else if ("酒店".equalsIgnoreCase(DictionaryModel.getInstance().getTypeByTypeId(area.type).typeName) || "住宅".equalsIgnoreCase(DictionaryModel.getInstance().getTypeByTypeId(area.type).typeName)) {
                    v.setColorView(Color.parseColor("#ff9966"));
                } else {
                    v.setColorView(Color.parseColor("#33cc99"));
                }
            }
        }
        if (mDragViewList.get(i) != null) {
            mDragViewList.get(i).setSelected(true);
            PlanModel.Area area = (PlanModel.Area) mDragViewList.get(i).getTag();
            if (area == null) {
                mDragViewList.get(i).setColorView(Color.parseColor("#339999"));
            } else {
                if ("办公".equalsIgnoreCase(DictionaryModel.getInstance().getTypeByTypeId(area.type).typeName)) {
                    mDragViewList.get(i).setColorView(Color.parseColor("#339999"));
                } else if ("酒店".equalsIgnoreCase(DictionaryModel.getInstance().getTypeByTypeId(area.type).typeName) || "住宅".equalsIgnoreCase(DictionaryModel.getInstance().getTypeByTypeId(area.type).typeName)) {
                    mDragViewList.get(i).setColorView(Color.parseColor("#ff5a0b"));
                } else {
                    mDragViewList.get(i).setColorView(Color.parseColor("#339999"));
                }
            }
        }
    }

    public TextView getDragViewFenQuText(int index) {
        return mDragViewList.get(index).getFenquText();
    }

    public TextView getDragViewOtherInfoText(int index) {
        return mDragViewList.get(index).getOtherInfo();
    }
}
